import { Controller, Post, Body, Get, UseGuards } from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
} from '@nestjs/swagger';
import { AuthService } from './auth.service';
import { LoginDto } from './dto/login.dto';
import { RegisterDto } from './dto/register.dto';
import { Public } from '../../common/decorators/public.decorator';
import { User } from '../../common/decorators/user.decorator';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { User as UserEntity } from '../users/entities/user.entity';

/**
 * 认证控制器
 * 处理用户登录、注册、获取个人信息等接口
 */
@ApiTags('Auth')
@Controller('auth')
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  /**
   * 用户登录
   */
  @Public()
  @Post('login')
  @ApiOperation({ summary: '用户登录' })
  @ApiResponse({
    status: 200,
    description: '登录成功',
    schema: {
      example: {
        code: 0,
        message: 'success',
        data: {
          accessToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
          user: {
            id: 1,
            username: 'john_doe',
            email: 'john@example.com',
            nickname: 'John',
            avatar: null,
            isActive: true,
            createdAt: '2024-01-01T00:00:00.000Z',
            updatedAt: '2024-01-01T00:00:00.000Z'
          }
        },
        timestamp: '2024-01-01T00:00:00.000Z'
      }
    }
  })
  @ApiResponse({
    status: 401,
    description: '用户名或密码错误',
    schema: {
      example: {
        code: 401,
        message: '用户名或密码错误',
        data: null,
        timestamp: '2024-01-01T00:00:00.000Z',
        path: '/api/auth/login'
      }
    }
  })
  async login(@Body() loginDto: LoginDto) {
    return await this.authService.login(loginDto);
  }

  /**
   * 用户注册
   */
  @Public()
  @Post('register')
  @ApiOperation({ summary: '用户注册' })
  @ApiResponse({
    status: 201,
    description: '注册成功',
    schema: {
      example: {
        code: 0,
        message: 'success',
        data: {
          id: 1,
          username: 'john_doe',
          email: 'john@example.com',
          nickname: 'John',
          avatar: null,
          isActive: true,
          createdAt: '2024-01-01T00:00:00.000Z',
          updatedAt: '2024-01-01T00:00:00.000Z'
        },
        timestamp: '2024-01-01T00:00:00.000Z'
      }
    }
  })
  @ApiResponse({
    status: 409,
    description: '用户名或邮箱已存在',
    schema: {
      example: {
        code: 409,
        message: '用户名或邮箱已存在',
        data: null,
        timestamp: '2024-01-01T00:00:00.000Z',
        path: '/api/auth/register'
      }
    }
  })
  async register(@Body() registerDto: RegisterDto) {
    return await this.authService.register(registerDto);
  }

  /**
   * 获取当前用户信息
   */
  @UseGuards(JwtAuthGuard)
  @Get('profile')
  @ApiBearerAuth()
  @ApiOperation({ summary: '获取当前用户信息' })
  @ApiResponse({
    status: 200,
    description: '获取用户信息成功',
    schema: {
      example: {
        code: 0,
        message: 'success',
        data: {
          id: 1,
          username: 'john_doe',
          email: 'john@example.com',
          nickname: 'John',
          avatar: null,
          isActive: true,
          createdAt: '2024-01-01T00:00:00.000Z'
        },
        timestamp: '2024-01-01T00:00:00.000Z'
      }
    }
  })
  @ApiResponse({
    status: 401,
    description: '未授权访问',
    schema: {
      example: {
        code: 401,
        message: '未授权访问',
        data: null,
        timestamp: '2024-01-01T00:00:00.000Z',
        path: '/api/auth/profile'
      }
    }
  })
  getProfile(@User() user: UserEntity) {
    return user;
  }
}